package echos

import (
	"io"; "fmt"; "path/filepath" 
  //"os"; "log"
  "io/ioutil"
	"github.com/labstack/echo"
	"html/template"
)

// TemplateRenderer is a custom html/template renderer for Echo framework
type TemplateRenderer struct {
	templates *template.Template
}

// Render renders a template document
func (t *TemplateRenderer) Render(w io.Writer, name string, 
								  data interface{}, 
								  c echo.Context) error {

	return t.templates.ExecuteTemplate(w, name, data)
}


func APP_GetAllFile(pathname string) error {
    rd, err := ioutil.ReadDir(pathname)
    for _, fi := range rd {
        if fi.IsDir() {
            //fmt.Printf("[%s]\n", pathname+"\\"+fi.Name())
            APP_GetAllFile(pathname + fi.Name() + "\\")
        } else {
            fmt.Println(fi.Name())
        }
    }
    return err
}

/*
 html 模板池;
 原则上所有的html 页面都做成模板形式
 */
func APP_TemplatesPool(e *echo.Echo, t string) error {
  /*
	cur_dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
		var templates_loc string
		if err != nil {
			log.Fatal(err)
			return err
		}else{
			templates_loc = cur_dir+"/"+t
		}
  */
  var err error
  	path, fileName := filepath.Split(t)
  	fmt.Printf("Template files location: %s\n", path)
  	fmt.Printf("Template files type: %s\n", fileName)
  	err = APP_GetAllFile(path)

  	renderer := &TemplateRenderer{
      	templates: template.Must(template.ParseGlob(t)),
  	}

  	e.Renderer = renderer

  	return err
}